#ifndef CHUNKFILEREADER_H
#define CHUNKFILEREADER_H

#include <QObject>
#include <QFile>
#include <QTimer>
#include <QDebug>

class ChunkFileReader : public QObject
{
    Q_OBJECT
public:
    explicit ChunkFileReader(QObject *parent = nullptr);

    /**
     * @brief start     打开文件并开始分块读取
     * @param filePath
     * @param chunkSize
     * @return
     */
    bool start(const QString& filePath, qint64 chunkSize = 1 * 1024);

signals:
    /**
     * @brief chunkReady 每块数据准备好
     * @param chunk
     */
    void chunkReady(const QByteArray &chunk);
    /**
     * @brief finished  发送完成
     */
    void finished();
    /**
     * @brief progress      进度
     * @param bytesSent
     * @param total
     */
    void progress(qint64 bytesSent, qint64 total);

private slots:
    void sendNextChunk();

private:
    /**
     * @brief m_file 文件对象
     */
    QFile m_file;

    /**
     * @brief m_chunkSize 发送块大小
     */
    qint64 m_chunkSize = 4 * 1024;  // 默认4KB
    /**
     * @brief m_totalSize 文件总的字节大小
     */
    qint64 m_totalSize = 0;
    /**
     * @brief m_bytesSent 已发送字节数
     */
    qint64 m_bytesSent = 0;
};

#endif // CHUNKFILEREADER_H
